<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Article extends CI_Controller {
	public $page_Name='article';
	public function __construct(){
		parent::__construct();
		if(!$this->session->userdata('is_login')){
			redirect('/admin/auth/login');
		}
	}
	public $validation_config = array(
		    array(
		        'field' => 'title',
		        'label' => '标题',
		        'rules' => 'required'
		    ),
		    array(
		        'field' => 'catalog_id',
		        'label' => '类别',
		        'rules' => 'required',
		    ),
		    array(
		        'field' => 'img',
		        'label' => '图片',
		        'rules' => 'required'
		    ),
		    array(
		        'field' => 'desc',
		        'label' => '描述',
		        'rules' => 'required'
		    ),
		     array(
		        'field' => 'content',
		        'label' => '内容',
		        'rules' => 'required'
		    )
		);
    public function index($page=1){
    	$pageSize=5;
    	$this->load->model('Article_model');
    	$total=$this->Article_model->count_all();
    	$articles=$this->Article_model->get_page($page,$pageSize);
    	$data=array(
			'articles'=>$articles,
			'pageName'=>'article'
		);
		$this->load->library('pagination');
		$config['use_page_numbers'] = TRUE;
		$config['base_url'] =site_url('admin/Article/index/');
		$config['total_rows'] = $total;
		$config['per_page'] = $pageSize;
        $this->pagination->initialize($config);
    	$this->load->view('admin/admin_article',$data);
    }
    public function del($id){
    	$this->load->model('Article_model');
    	$this->Article_model->del($id);
    	redirect("/admin/Article/index");
    }
    public function add(){
	    $this->load->library('form_validation');
	   $this->form_validation->set_rules($this->validation_config);
	    $this->load->model('Article_model');
	    $article= new stdClass();
	    $article->title='';
	    $article->catalog_id='';
	    $article->img='';
	    $article->desc='';
	    $article->content='';
	    if ($this->form_validation->run() == FALSE){
	    	$this->load->model('Catalog_model');
	    	$data=array(
	    		'catalogs'=>$this->Catalog_model->get_all(),
	    		'article'=>$article,
	    		'a'=>'添加',
	    		);
	    	$this->load->view('admin/article_form',$data);
	    }else{
	    	$array=$this->input->post();
	    	$array['create_time']=time();
	    	$this->Article_model->add($array);
			redirect("/admin/Article/index");
	    }
    }
    public function edit($id){
    	$this->load->library('form_validation');
	    $this->form_validation->set_rules($this->validation_config);
	    $this->load->model('Article_model');
	    $this->load->model('Catalog_model');
	    if ($this->form_validation->run() == FALSE){
	    	$data=array(
	    		'catalogs'=>$this->Catalog_model->get_all(),
	    		'article'=>$this->Article_model->get_one($id),
	    		'a'=>'修改'
	    		);
	    	$this->load->view('admin/article_form',$data);
	    }else{
	    	$data=array(
	    		'catalogs'=>$this->Catalog_model->get_all(),
	    		'articles'=>$this->Article_model->get_one($id),
	    		'a'=>'修改'
	    		);
	    	$this->load->view('admin/article_form',$data);
	    	$array=$this->input->post();
	    	$this->Article_model->edit($array,$id);
          	redirect("/admin/Article/index");
	    }
    }
    public function article_recommend($id){
    	$this->load->model('Article_model');
    	$this->Article_model->get_article_recommend($id);
    	redirect("/admin/Article/index");
    }

    //如要用下面写法，则要改“推荐/取消”的链接echo site_url('admin/article/article_recommend/'.$value->id.'/0')
    //$value为recommend的值，0为不推荐，1为推荐 
    // public function article_recommend($id,$value){
    // 	$this->load->model('Article_model');
    // 	$this->Article_model->get_recommend($id,$value);
    // 	redirect("/admin/Article/index");
    // }
}
